// pages/eventsDetail/eventsDetail.js
import profile from '../../profile'

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    lang: app.globalData.language,

    index: profile.eventsDetail,

    videoIsPlay: false,

    eventsLike: profile.eventsDetail.eventsLike,
    likeNumber: profile.eventsDetail.likeNumber,

    signinList: profile.eventsDetail.signinList,
    signinNumber: profile.eventsDetail.signinNumber,
    signBtnText: profile.eventsDetail.signBtnText,
    signBtnText_en: profile.eventsDetail.signBtnText_en,
    icon: profile.eventsDetail.icon,
    style: profile.eventsDetail.style,
    disabled: profile.eventsDetail.disabled,

    // 评论相关
    isCommentOpen: false,
    commentValue: '',

    // 弹窗相关
    popShow: false,
    popTile: '',
    popContent: [],
    popBtn: [],

    // 报名相关
    openEnroll: false,
    enrollName: '',
    enrollAge: '',
    ageArray: profile.eventsDetail.AgeArray,
    ageIndex: 0,
    enrollPhone: '',
    enrollJob: '',
    jobArray: [],
    jobIndex: 0,
    enrollSex: '',
    sexArray: profile.eventsDetail.sexArray,
    enrollNum: '',
    numArray: profile.eventsDetail.numArray,
    numIndex: 0,
    enrollTime: '',
    timeArray: profile.eventsDetail.timeArray,
    timeIndex: 0,

    keyBoardHeight: 0
  },

  keyBoardHeight: function (e) {
    //console.log(e.detail.height);
    this.setData({
      keyBoardHeight: e.detail.height
    })
  },

  // 报名提交按钮事件
  submitEnrollTap: function (e) {
    let name = this.data.enrollName, age = this.data.enrollAge, phone = this.data.enrollPhone, job = this.data.enrollJob, sex = this.data.enrollSex, num = this.data.enrollNum, time = this.data.enrollTime,
    popTitle = '', popContentText = '', popBtnText = '';
    let list = profile.eventsDetail.signinList, number = profile.eventsDetail.signinNumber, img = profile.eventsDetail.userImg;
    if (time == '' || num == '' || sex == '' || job == '' || phone == '' || age == '' || name == '') {
      if (this.data.lang == 'cn') {
        if (time == '') {
          popContentText = '请选择您预约的时间！';
        }
        if (num == '') {
          popContentText = '请选择您参加的人数！';
        }
        if (sex == '') {
          popContentText = '请选择您的性别！';
        }
        if (job == '') {
          popContentText = '请选择您的职业！';
        }
        if (phone == '') {
          popContentText = '请输入您的手机号码！';
        }
        if (age == '') {
          popContentText = '请选择您的年龄！';
        }
        if (name == '') {
          popContentText = '请输入您的姓名！';
        }
        popTitle = '抱歉！';
        popBtnText = '确认';
      } else {
        if (time == '') {
          popContentText = 'Please choose the time of your reservation!';
        }
        if (num == '') {
          popContentText = 'Please select the number of participants!';
        }
        if (sex == '') {
          popContentText = 'Please choose your gender!';
        }
        if (job == '') {
          popContentText = 'Please choose your career!';
        }
        if (phone == '') {
          popContentText = 'Please input your mobile phone number!';
        }
        if (age == '') {
          popContentText = 'Please choose your age!';
        }
        if (name == '') {
          popContentText = 'Please enter your name!';
        }
        popTitle = 'sorry！';
        popBtnText = 'comfirm';
      }
      this.setData({
        popTile: popTitle,
        popContent: [{
          popTextCenter: true,
          text: popContentText
        }],
        popBtn: [{
          id: 0,
          text: popBtnText
        }],
        popShow: true,
      })
      return;
    }

    let successTitle = '', successText = '', successBtn = '';
    if (this.data.lang == 'cn') {
      successTitle = '报名成功';
      successText = '您已成功报名“'+ profile.eventsDetail.name +'”活动。活动现场打卡可获得积分奖励，兑换礼品哦！';
      successBtn = '确定';
    } else {
      successTitle = 'Successful registration';
      successText = 'You have successfully signed up for "'+ profile.eventsDetail.name_en +'" activity. You can get bonus points and exchange gifts when you punch in at the activity site!';
      successBtn = 'confirm';
    }
    list.shift();
    list.push(img);
    number --;
    this.setData({
      popTile: successTitle,
      popContent: [{
        popTextCenter: false,
        text: successText
      }],
      popBtn: [{
        id: 0,
        text: successBtn
      }],
      popShow: true,
      openEnroll: false,
      signinList: list,
      signinNumber: number,
      signBtnText: '取消预约',
      signBtnText_en: 'cancel reservation',
      icon: '../../images/cancel_icon.png',
      style: 'col'
    })
    profile.eventsDetail.signinList = list;
    profile.eventsDetail.signBtnText = '取消预约';
    profile.eventsDetail.signBtnText_en = 'cancel';
    profile.eventsDetail.signinNumber = number;
    profile.eventsDetail.icon = '../../images/sign_in_icon.png';
    profile.eventsDetail.style = 'col';
  },

  dateEnd: function (e) {
    console.log(e.detail)
  },

  // 打开报名按钮事件
  signInTap: function (e) {
    let signBtnText = this.data.signBtnText;
    let popTitle = '', popText = [], popBtn = [];
    if (signBtnText == '取消预约' || signBtnText == 'cancel') {
      if (this.data.disabled) {
        if (app.globalData.language == 'cn') {
          popTitle = '取消失败';
          popText = [{
            popTextCenter: false,
            text: '本次活动开始前24小时，不可取消预约。'
          }];
          popBtn = [{
            id: 0,
            text: '确定'
          }];
        }else{
          popTitle = 'Cancellation failed';
          popText = [{
            popTextCenter: false,
            text: 'No cancellation is allowed 24 hours before the event.'
          }];
          popBtn = [{
            id: 0,
            text: 'confirm'
          }];
        }
      }else{
        if (app.globalData.language == 'cn') {
          popTitle = '确认取消';
          popText = [{
            popTextCenter: false,
            text: '确认取消本次活动预约？'
          },{
            popTextCenter: false,
            text: '温馨提示：活动开始前24小时，无法取消预约；'
          }];
          popBtn = [{
            id: 'comfirmCancel',
            text: '确定'
          },{
            id: 0,
            text: '取消'
          }]
        }else{
          popTitle = 'Confirm cancellation';
          popText = [{
            popTextCenter: false,
            text: 'Are you sure you want to cancel the appointment?'
          },{
            popTextCenter: false,
            text: 'Warm tips: you can\'t cancel the appointment 24 hours before the activity starts;'
          }];
          popBtn = [{
            id: 'comfirmCancel',
            text: 'comfirm'
          },{
            id: 0,
            text: 'cancel'
          }]
        }
      }
      this.setData({
        popTile: popTitle,
        popContent: popText,
        popBtn: popBtn,
        popShow: true,
      })
      return;
    }
    if (signBtnText == '主办单位我有话想说' || signBtnText == 'talk to Organizer') {
      return;
    }
    if (signBtnText == '打卡失败' || signBtnText == 'Failed Check') {
      return;
    }
    this.setData({
      openEnroll: true,
    })
  },

  // 报名预约时间选择事件
  bindPickerChangeTime: function(e) {
    let timeArr = this.data.timeArray;
    this.setData({
      enrollTime: timeArr[e.detail.value]
    })
  },

  // 报名人数选择事件
  bindPickerChangeNum: function(e) {
    let numArr = this.data.numArray;
    this.setData({
      enrollNum: numArr[e.detail.value]
    })
  },

  // 报名性别选择事件
  radioChange(e) {
    let items = this.data.sexArray, sex = '';
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].value === e.detail.value
      if (items[i].checked) {
        sex = items[i].value;
      }
    }
    this.setData({
      enrollSex: sex,
      sexArray: items
    })
  },

  // 报名职业选择事件
  bindPickerChangeJob: function(e) {
    let jobArr = this.data.jobArray;
    this.setData({
      enrollJob: jobArr[e.detail.value]
    })
  },

  // 报名手机输入侦听事件
  enrollPhoneInput: function(e) {
    let value = e.detail.value;
    if (/^0?1[3|5|8][0-9]\d{8}$/.test(value)){
      console.log(true)
    }else{
      console.log(false)
    }
    this.setData({
      enrollPhone: value
    })
  },

  // 报名年龄选择事件
  bindPickerChangeAge: function(e) {
    let ageArr = this.data.ageArray;
    this.setData({
      enrollAge: ageArr[e.detail.value]
    })
  },

  // 报名姓名输入侦听事件
  enrollNameInput: function(e) {
    let value = e.detail.value;
    this.setData({
      enrollName: value
    })
  },

  // 弹窗点击事件
  popBtnTap: function(e) {
    let id = e.detail.id, style = '', icon = '', signBtnText = '', signBtnText_en = '';
    if (id == 'comfirmCancel') {
      console.log('确认取消预约');
      icon = '../../images/sign_in_icon.png';
      signBtnText = '报名';
      signBtnText_en = 'enroll';
      this.setData({
        style,
        icon,
        signBtnText,
        signBtnText_en
      })
    }
    this.setData({
      popShow: false,
    })
    profile.eventsDetail.signBtnText = signBtnText;
    profile.eventsDetail.signBtnText_en = signBtnText_en;
    profile.eventsDetail.icon = '../../images/sign_in_icon.png';
    profile.eventsDetail.style = '';
  },

  // 提交评论事件
  submitCommentTap: function(e) {
    let value = this.data.commentValue, popTitle = '', popContentText = '', popBtnText = '';
    if (value == ''){
      if (app.globalData.language == 'cn') {
        popTitle = '抱歉！';
        popContentText = '请输入您的评论！';
        popBtnText = '确认';
      } else {
        popTitle = 'sorry！';
        popContentText = 'Please enter your comments!';
        popBtnText = 'comfirm';
      }
      this.setData({
        popTile: popTitle,
        popContent: [{
          popTextCenter: true,
          text: popContentText
        }],
        popBtn: [{
          id: 0,
          text: popBtnText
        }],
        popShow: true,
      })
      return;
    }
    this.setData({
      isCommentOpen: false,
      commentValue: ''
    })
  },

  // 评论输入侦听事件
  commentInput: function(e) {
    let value = e.detail.value;
    this.setData({
      commentValue: value
    })
  },

  // 关闭评论开关事件
  closeCommentTap: function(e){
    this.setData({
      openEnroll: false,
      isCommentOpen: false
    })
  },

  // 打开评论开关事件
  openCommentTap: function(e){
    this.setData({
      isCommentOpen: true
    })
  },

  // 艺术活动like点击事件
  eventsLikeTap: function (e) {
    let like = this.data.eventsLike, num = this.data.likeNumber;
    if (like){
      like = false
      num --;
    }else{
      like = true
      num ++;
    }
    this.setData({
      eventsLike: like,
      likeNumber: num
    })
    profile.eventsDetail.eventsLike = like;
    profile.eventsDetail.likeNumber = num;
  },

  // 视频播放完毕
  videoComplete: function (e) {
    this.setData({
      videoIsPlay: false
    })
  },

  // 视频点击播放事件
  videoPlayTap: function(e) {
    let videoObj = wx.createVideoContext('video');
    videoObj.play();
    this.setData({
      videoIsPlay: true
    })
  },

  // 语言切换事件
  menuTap: function (e) {
    this.setData({
      lang: e.detail.lang
    })
    if (app.globalData.language == 'cn'){
      wx.setNavigationBarTitle({
        title: '艺术活动'
      })
    }else{
      wx.setNavigationBarTitle({
        title: 'Art Events'
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let jobArr = this.data.jobArray;
    if (app.globalData.language == 'cn'){
      jobArr = profile.eventsDetail.JobArray;
    }else{
      jobArr = profile.eventsDetail.JobArray_en;
    }
    this.setData({
      jobArray: jobArr
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      lang: app.globalData.language,
    })
    if (app.globalData.language == 'cn'){
      wx.setNavigationBarTitle({
        title: '艺术活动'
      })
    }else{
      wx.setNavigationBarTitle({
        title: 'Art Events'
      })
    }
  },
})